﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Management; // 需要引用System.Management.dll
namespace Yb
{
    public static class WindowsInfo
    {
        private static string _machineid = "";
        //获得设备唯一码,这个唯一码跟操作系统，登录用户名和设备相关[CPU,主板，硬盘，网卡]
        public static string GetMachineId()
        {
            if (_machineid != "")
            {
                return _machineid;
            }
            var info = GetSystemInformation();
            string combinedInfo = $"{info.UserName}-{info.WindowsVersion}-{info.UniqueMachineId}";

            // 使用 SHA256 生成唯一标识符
            using (SHA256 sha256 = SHA256.Create())
            {
                byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(combinedInfo));
                StringBuilder hashBuilder = new StringBuilder();
                foreach (byte b in hashBytes)
                {
                    hashBuilder.Append(b.ToString("x2"));
                }
                return hashBuilder.ToString();
            }
        }
        public static (string WindowsVersion, string UniqueMachineId, string UserName) GetSystemInformation()
        {
            // 获取Windows版本
            string windowsVersion = GetWindowsVersion();

            // 获取电脑唯一码
            string uniqueMachineId = GetUniqueMachineId();

            // 获取当前Windows用户名
            string userName = Environment.UserName;

            return (windowsVersion, uniqueMachineId, userName);
        }

        private static string GetWindowsVersion()
        {
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem"))
            {
                foreach (ManagementObject os in searcher.Get())
                {
                    return os["Caption"]?.ToString() ?? "未知Windows版本";
                }
            }
            return "未知Windows版本";
        }
        private static string GetMacAddress()
        {
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT MACAddress FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL"))
            {
                foreach (ManagementObject network in searcher.Get())
                {
                    return network["MACAddress"]?.ToString() ?? "未知";
                }
            }
            return "未知";
        }
        private static string GetUniqueMachineId()
        {
            // 获取多种硬件和系统信息
            string cpuId = GetHardwareIdentifier("Win32_Processor", "ProcessorId");
            string motherboardSerial = GetHardwareIdentifier("Win32_BaseBoard", "SerialNumber");
            string diskSerial = GetHardwareIdentifier("Win32_DiskDrive", "SerialNumber");
            string macAddress = GetMacAddress();
            string hostName = Environment.MachineName;

            // 组合所有信息
            string combinedInfo = $"{cpuId}-{motherboardSerial}-{diskSerial}-{macAddress}-{hostName}";

            // 使用 SHA256 生成唯一标识符
            using (SHA256 sha256 = SHA256.Create())
            {
                byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(combinedInfo));
                StringBuilder hashBuilder = new StringBuilder();
                foreach (byte b in hashBytes)
                {
                    hashBuilder.Append(b.ToString("x2"));
                }
                return hashBuilder.ToString();
            }
        }

        private static string GetHardwareIdentifier(string className, string propertyName)
        {
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher($"SELECT {propertyName} FROM {className}"))
            {
                foreach (ManagementObject hardware in searcher.Get())
                {
                    return hardware[propertyName]?.ToString() ?? "未知";
                }
            }
            return "未知";
        }
    }
}
